package it11.Capood.GUI;

import java.util.ArrayList;
import java.util.List;

import it11.Capood.R;
import it11.Capood.DataLogic.Message;
import it11.Capood.DataLogic.Message.Type;
import it11.Capood.DataLogic.Storage;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class HistoryActivity extends Activity {

	private ListView savedMessages;
	private ArrayAdapter<Message> adapterForMessageListView;
	private Message choosenOpenMessage;
	private Message choosenDeleteMessage;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_history);
		
		savedMessages = (ListView) findViewById(R.id.saved_messages_list);
		
		
				
	}
	
	@Override
	public void onStart(){
		super.onStart();
		
		if (Storage.getInstance(this).getInternalSaveDataFile().exists()) {
			adapterForMessageListView = new MessageAdapter(this, R.layout.history_item,new ArrayList<Message>(Storage.getInstance(this).getMessageList()));
			savedMessages.setAdapter(adapterForMessageListView);
			savedMessages.setOnItemClickListener(deleteSavedMessage);
			savedMessages.setOnItemLongClickListener(openSavedMessage);
		} else {
			savedMessages.setEnabled(false);
		}
	}
	

	private OnItemClickListener deleteSavedMessage = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			choosenDeleteMessage = (Message) savedMessages.getAdapter().getItem(arg2);
		}
	};
	
	private OnItemLongClickListener openSavedMessage = new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {
			choosenOpenMessage = (Message) savedMessages.getAdapter().getItem(arg2);
			if (choosenOpenMessage.getType() == Type.TEXT) {
				newReceiveTextActivity();
			} else if (choosenOpenMessage.getType() == Type.FILE_INFO) {
				newReceiveFileActivity();
			} else {
				Toast.makeText(getApplicationContext(), "Unable to open the Message", Toast.LENGTH_LONG).show();
			}
			return false;
		}
	};
	
	public void newReceiveTextActivity () {
		Intent intent = new Intent (this,ReceivingTextActivity.class);
		intent.putExtra(ReceivingTextActivity.EXTRA_MESSAGE, choosenOpenMessage);
		startActivity(intent);
	}
	
	public void newReceiveFileActivity () {
		Intent intent = new Intent (this,ReceivingFileActivity.class);
		intent.putExtra(ReceivingFileActivity.EXTRA_FILE_INFO, choosenOpenMessage);
		startActivity(intent);
	}

	public void deleteChecked (View view) {
		Storage.getInstance(this).deleteMessageFromList(choosenDeleteMessage);
		adapterForMessageListView.notifyDataSetChanged();
		Toast.makeText(this, "Deleting succeed", Toast.LENGTH_SHORT).show();
	}
}
